package com.cherry.androidviewdemo.widget

import android.content.Context
import android.graphics.*
import android.support.annotation.ColorInt
import android.util.AttributeSet
import android.view.View
import com.cherry.androidviewdemo.R
import com.cherry.androidviewdemo.utils.dp2px
import com.cherry.androidviewdemo.utils.sp2px

class QQStepView @JvmOverloads constructor(context: Context, attrs: AttributeSet? = null, defStyleAttr: Int = 0) : View(context, attrs, defStyleAttr) {

    @ColorInt
    var innerColor: Int

    @ColorInt
    var outColor: Int

    var stepTextSize: Int

    @ColorInt
    var stepTextColor: Int

    var borderWidth: Float

    private val rectF: RectF

    private val textRect: Rect

    private val outPaint: Paint
    private val innerPaint: Paint
    private val textPaint: Paint

    var currentStep = 0
        set(value) {
            field = value
            invalidate()
        }

    var maxStep = 0

    init {

        val typedArray = context.obtainStyledAttributes(attrs, R.styleable.QQStepView)
        innerColor = typedArray.getColor(R.styleable.QQStepView_innerColor, Color.BLUE)
        outColor = typedArray.getColor(R.styleable.QQStepView_outerColor, Color.YELLOW)
        stepTextSize = typedArray.getDimensionPixelSize(R.styleable.QQStepView_stepTextSize, 16f.sp2px(context).toInt())
        borderWidth = typedArray.getDimension(R.styleable.QQStepView_borderWidth, 5f.dp2px(context))
        stepTextColor = typedArray.getColor(R.styleable.QQStepView_stepTextColor, Color.BLUE)
        typedArray.recycle()

        outPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        outPaint.color = outColor
        outPaint.style = Paint.Style.STROKE
        outPaint.strokeWidth = borderWidth
        outPaint.strokeCap = Paint.Cap.ROUND

        innerPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        innerPaint.color = innerColor
        innerPaint.style = Paint.Style.STROKE
        innerPaint.strokeWidth = borderWidth
        innerPaint.strokeCap = Paint.Cap.ROUND

        textPaint = Paint(Paint.ANTI_ALIAS_FLAG)
        textPaint.color = stepTextColor
        textPaint.textSize = stepTextSize.toFloat()

        rectF = RectF()
        textRect = Rect()
    }


    override fun onMeasure(widthMeasureSpec: Int, heightMeasureSpec: Int) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec)
        //确保形状是正方形
        val width = MeasureSpec.getSize(widthMeasureSpec) + paddingLeft + paddingRight
        val height = MeasureSpec.getSize(heightMeasureSpec) + paddingTop + paddingBottom
        val size = width.coerceAtLeast(height)
        setMeasuredDimension(size, size)
    }

    override fun onDraw(canvas: Canvas) {
        super.onDraw(canvas)
        val center = width / 2f
        val radius = center - borderWidth / 2f

        //画外圆弧
        rectF.set(center - radius + paddingLeft, center - radius + paddingTop, center + radius - paddingRight, center + radius - paddingBottom)
        canvas.drawArc(rectF, 135f, 270f, false, outPaint)

        if (maxStep <= 0 && currentStep > maxStep) {
            return
        }

        //画内圆弧
        val percent = currentStep * 1f / maxStep
        canvas.drawArc(rectF, 135f, 270f * percent, false, innerPaint)

        //画中心指示文字
        val stepText = currentStep.toString()
        textPaint.getTextBounds(stepText, 0, stepText.length, textRect)
        val dx = (width - textRect.width()) / 2f

        val fontMetrics = textPaint.fontMetrics
        val dy = (fontMetrics.bottom - fontMetrics.top) / 2f - fontMetrics.bottom
        val baseLine = height / 2f + dy
        canvas.drawText(stepText, dx, baseLine, textPaint)

    }


}